﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Farigola.Engine.SystemData.Entity.Type;
using Farigola.Engine.SystemData.Entity;
using Farigola.Engine.SystemData.Entity.inst;
using Farigola.Engine.SystemData.DataValue;

namespace Farigola.Engine.TestUnit
{
    /// <summary>
    /// Test the entity string, type and instance.
    /// </summary>
    [TestFixture]
    public class EntityStringTestUnit 
    {
	/**
	 * Check that the type string is present in the internal manager of type.
	 */
	[Test]
	public void testTypeString() {
        Engine engine = EngineCommonTestUnit.CreateInitEngine();
        Workspace wksp = engine.GetWorkspace();

		// get/find the type string from the system data mgr from the workspace, by the name 
		EntTypeString typeString = (EntTypeString) wksp.GetSystemDataMgr().FindEntTypeByName(EntityDef.TypeStringName);
		Assert.IsNotNull(typeString, "The type named 'string' should exists.");

		// get/find the type string from the system data mgr from the workspace, by the id 
		EntityId id= new EntityId();
		id.SetFromIdStr(EntityDef.TypeStringValGuid);
		typeString = (EntTypeString) wksp.GetSystemDataMgr().FindEntTypeById(id);
		Assert.IsNotNull(typeString, "The type string with the dedicated id should exists.");
	}
	
	
	/**
	 * basic test of the string entity.
	 */
	[Test]
	public void testCreEntStringByName() {
        Engine engine = EngineCommonTestUnit.CreateInitEngine();
        Workspace wksp = engine.GetWorkspace();
		
		// create a project
		Project prj= wksp.CreateActiveProject("prj");

		// create a string under the root folder user 
		EntInstString entString = prj.createEntInstString(null, "str");
		Assert.IsNotNull(entString, "The string 'str' should exists.");
				
		// set a value to the string
		// todo:
		
		// get the tool to find entities in the project
		EntityFinder entFinder= prj.GetEntityFinder();
				
		// check that the ent string is present under the root folder user
		EntityBase ent= entFinder.FindEntityByName(EntityFinder.FindSystemUserType.User, prj.GetRootFolder(), "str",true);
		entString= (EntInstString)ent;
		Assert.IsNotNull(entString,"The string 'str' should exists under the root folder user.");
	}
	
	/**
	 * basic test of the EntityFinder.
	 */
	[Test]
	public void testCreEntStringUnderRootFolderUserByDefault() {
        Engine engine = EngineCommonTestUnit.CreateInitEngine();
        Workspace wksp = engine.GetWorkspace();
		
		// create a project
		Project prj= wksp.CreateActiveProject("prj");

		// create a string under the root folder user 
		EntInstString entString = prj.createEntInstString("str");
		Assert.IsNotNull(entString, "The string 'str' should exists.");
		
		// get the tool to find entities in the project
		EntityFinder entFinder= prj.GetEntityFinder();
		
		// check that the ent string is present under the root folder user
		EntityBase ent= entFinder.FindEntityByName(EntityFinder.FindSystemUserType.User, prj.GetRootUserFolder(), "str",true);
		entString= (EntInstString)ent;
		Assert.IsNotNull(entString, "The string 'str' should exists under the root folder user.");
	}

    /// <summary>
    /// set a string to an entity instance string.
    /// </summary>
	[Test]
	public void testEntStringSetVal() {
        Engine engine = EngineCommonTestUnit.CreateInitEngine();
        Workspace wksp = engine.GetWorkspace();
		
		// create a project
		Project prj= wksp.CreateActiveProject("prj");

		// create a string under the root folder user 
		EntInstString entString = prj.createEntInstString("str");
		Assert.IsNotNull(entString, "The string 'str' should exists.");
				
		// set a value to the string, through the project, not directly 
		bool res= prj.SetValToEntInstString(entString, "bonjour");
		Assert.IsTrue(res,"The set of the value of ent string should be possible.");
		
		// get and check the value
		string val= prj.GetValFromEntInstString(entString);
		Assert.AreEqual("bonjour", val,"The value of ent string should be: 'bonjour'.");
	}
	
	/**
	 * set a string to a data value string,
	 * the set the data value string to an entity instance string.
	 */
	[Test]
	public void testEntStringSetDataValue() {
        Engine engine = EngineCommonTestUnit.CreateInitEngine();
        Workspace wksp = engine.GetWorkspace();
		
		// create a project
		Project prj= wksp.CreateActiveProject("prj");
		
		// create a data value object
		DataValueString dvStr= prj.CreateDataValueString("hello");
		Assert.IsNotNull(dvStr, "The data value string 'dvStr' should exists.");
		
		// create a string under the root folder user 
		EntInstString entString = prj.createEntInstString("str",dvStr);
		Assert.IsNotNull(entString,"The string 'str' should exists.");
		
		// get and check the value
		dvStr= prj.GetDataValueFromEntInstString(entString);
		Assert.IsNotNull(dvStr, "The data value string 'dvStr' should exists.");

		// check the string value of the data value string
		String val= dvStr.GetValue();
		Assert.AreEqual("hello", val, "The value of ent string should be: 'hello'.");		
	}
	
	// create and set with same method the string value?
	// createEntInstString(name, strval)
	
	// create and set with same method  the data value string ?
	// createEntInstString(name, dvStr)

    }
}
